﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChatLog
{
    public partial class ChatLogUI : Form
    {
        private int addsize = 0;
        public ChatLogUI()
        {
            InitializeComponent();
            this.AutoSize = true;
        }

        private void ChatLogUI_Load(object sender, EventArgs e)
        {
            ChatLogSettings.Instance.Load();
            cbGuild.Checked = ChatLogSettings.Instance.Guild;
            cbMaskNames.Checked = ChatLogSettings.Instance.MaskNames;
            cbSay.Checked = ChatLogSettings.Instance.Say;
            cbWhisper.Checked = ChatLogSettings.Instance.Whisper;
            cbYell.Checked = ChatLogSettings.Instance.Yell;
        }

        private void cbWhisper_CheckedChanged(object sender, EventArgs e)
        {
            ChatLogSettings.Instance.Whisper = cbWhisper.Checked;
            ChatLogSettings.Instance.Save();
        }

        private void cbGuild_CheckedChanged(object sender, EventArgs e)
        {
            ChatLogSettings.Instance.Guild = cbGuild.Checked;
            ChatLogSettings.Instance.Save();
        }

        private void cbSay_CheckedChanged(object sender, EventArgs e)
        {
            ChatLogSettings.Instance.Say = cbSay.Checked;
            ChatLogSettings.Instance.Save();
        }

        private void cbYell_CheckedChanged(object sender, EventArgs e)
        {
            ChatLogSettings.Instance.Yell = cbYell.Checked;
            ChatLogSettings.Instance.Save();
        }

        private void cbMaskNames_CheckedChanged(object sender, EventArgs e)
        {
            ChatLogSettings.Instance.MaskNames = cbMaskNames.Checked;
            ChatLogSettings.Instance.Save();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tbLog.Text = "";
            tbLog.Visible = true;
            if (File.Exists(ChatLog.LogFilePath))
            {
                tbLog.Text = System.IO.File.ReadAllText(ChatLog.LogFilePath);
                var longest = "";
                foreach (var l in tbLog.Lines)
                {
                    if (l.Length > longest.Length)
                        longest = l;
                }

                Size size = TextRenderer.MeasureText(longest, tbLog.Font);
                tbLog.Width = size.Width + 20;

                this.Refresh();
            }        
        }

        private void bClearLog_Click(object sender, EventArgs e)
        {
            tbLog.Text = "Cleared!";
            tbLog.Visible = false;
            this.Refresh();
        }
    }
}
